package JavaSEAdvance.day07_Exception.D703_HomeWork;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Xiao
 * @date 2021/7/24
 */
public class Test01 {
    /**
     * 1.分析以下需求，并用代码实现：
     * 	(1)定义List集合，存入多个字符串
     * 	(2)删除集合中所有字符串"def"
     * 	(3)然后再利用迭代器遍历集合元素并输出
     */
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("abc");
        list.add("def");
        list.add("ghi");
        list.add("jkl");
        list.add("def");
        list.add("mno");
        System.out.println(list);
        System.out.println("--------");
        removeMethod1(list);
        System.out.println("--------");
        removeMethod2(list);
        System.out.println("--------");
        removeMethod3(list);
    }

    private static void removeMethod3(List<String> list) {
        list.removeIf(s -> "def".equals(s));
        System.out.println(list);
    }

    private static void removeMethod2(List<String> list) {
        Iterator<String> i = list.iterator();
        while (i.hasNext()) {
            if ("def".equals(i.next())) {
                i.remove();
            }
        }
        System.out.println(list);
    }

    private static void removeMethod1(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            if ("def".equals(list.get(i))) {
                list.remove(i);
            }
        }
        for (String s : list) {
            System.out.println(s);
        }
    }
}
